package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.HolderAdapter;
import com.nest.WorkNest.adapter.HolderAdapter.ViewCreator;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.tools.Tool;
import com.nest.WorkNest.view.ListViewForScrollView;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class QuotationDetailAct extends BaseActivity {
    @Bind(R.id.tv_quotation_fabric)
    TextView tvQuotationFabric;
    @Bind(R.id.tv_quotation_fabricCost)
    TextView tvQuotationFabricCost;
    @Bind(R.id.tv_quotation_accessory)
    TextView tvQuotationAccessory;
    @Bind(R.id.tv_quotation_accessoryCost)
    TextView tvQuotationAccessoryCost;
    @Bind(R.id.tv_quotation_other)
    TextView tvQuotationOther;
    @Bind(R.id.tv_quotation_otherCost)
    TextView tvQuotationOtherCost;
    @Bind(R.id.tv_quotation_quoteNote)
    TextView tvQuotationQuoteNote;
    @Bind(R.id.tv_quotation_splCost)
    TextView tvQuotationSplCost;
    @Bind(R.id.tv_quotation_splTime)
    TextView tvQuotationSplTime;
    private Context context = QuotationDetailAct.this;
    private ListViewForScrollView lv_product;
    private ArrayList<HashMap<String, String>> dataList = new ArrayList<>();
    private HashMap<String, String> data = new HashMap<>();
    private HolderAdapter<HashMap<String, String>> pAdapter;
    private String QuoId;
    private LayoutInflater layoutInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_quotation);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        initBar();
        layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View headView = layoutInflater.inflate(R.layout.headview_quotation, null);
        QuoId = getIntent().getStringExtra("quoteId");
        loadQuotationDetail();
        actionbar_side_tab_name.setVisibility(View.VISIBLE);
        actionbar_side_tab_name.setText("询价报价单");
        ll_iv_actionbar_tab_side_left.setVisibility(View.VISIBLE);
        tabBack();
        lv_product = (ListViewForScrollView) findViewById(R.id.lv_quotation_product);
        lv_product.setSelector(R.color.bg_transparent);
        lv_product.addHeaderView(headView);
        createAdapter();
        pAdapter.update(dataList);
    }


    public void createAdapter() {
        ViewCreator<HashMap<String, String>> pcreator = new ViewCreator<HashMap<String, String>>() {

            @Override
            public View createView(LayoutInflater inflater, int position,
                                   HashMap<String, String> map) {

                return LayoutInflater.from(context).inflate(R.layout.items_listview_quotation,
                        null);
            }

            @Override
            public void updateView(View view, int position, HashMap<String, String> map) {
                TextView tv_quotation_productname = (TextView) view
                        .findViewById(R.id.tv_quotation_productname);
                TextView tv_quotation_price = (TextView) view.findViewById(R.id.tv_quotation_price);
                TextView tv_quotation_batch = (TextView) view.findViewById(R.id.tv_quotation_batch);
                tv_quotation_productname.setText(map.get("name"));
                tv_quotation_price.setText(map.get("quotation"));
                tv_quotation_batch.setText(map.get("Number"));
            }
        };
        pAdapter = new HolderAdapter<>(this.getLayoutInflater(), pcreator);
        lv_product.setAdapter(pAdapter);
        pAdapter.update(dataList);
    }

    public void loadQuotationDetail() {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                if (Tool.getString(result, "resultCode").equals("0")) {
                    String quote = Tool.getString(result, "quote");
                    data = Tool.getMapByJsonObj(quote);
                    HashMap<String, String> min = new HashMap<>();
                    min.put("name", data.get("name"));
                    min.put("Number", data.get("minOrderNumber"));
                    min.put("quotation", data.get("quotation"));
                    HashMap<String, String> opt = new HashMap<>();
                    opt.put("Number", data.get("optOrderNumber"));
                    opt.put("quotation", data.get("quotation2"));
                    opt.put("name", data.get("name"));
                    dataList.add(min);
                    dataList.add(opt);
                    pAdapter.update(dataList);
                    tvQuotationFabric.setText(data.get("fabric"));
                    tvQuotationFabricCost.setText(data.get("fabricCost"));
                    tvQuotationAccessory.setText(data.get("accessory"));
                    tvQuotationAccessoryCost.setText(data.get("accessoryCost"));
                    tvQuotationOther.setText(data.get("other"));
                    tvQuotationOtherCost.setText(data.get("otherCost"));
                    tvQuotationQuoteNote.setText(data.get("quoteNote"));
                    tvQuotationSplCost.setText(data.get("splCost") + "元");
                    tvQuotationSplTime.setText(data.get("splTime") + "天");
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostQuoDetail(context, QuoId, ioAuthCallBack);

    }
}
